package com.apexsoft.assets.base.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author : Jian Shen
 * @version : V1.0
 * @date : 2018/9/3
 */
@Slf4j
public class Md5Utils {

    public static String md5Encode2Hex(String laws) {
        StringBuilder hexValue = new StringBuilder();
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] byteArray = laws.getBytes("UTF-8");
            byte[] md5Bytes = md5.digest(byteArray);

            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            log.error("md5Encode2Hex ERROR: ", e);
        }

        return hexValue.toString();
    }
}
